//
//  LeftSideView.m
//  YaHooFrame
//
//  Created by dg11185_zal on 14/11/24.
//  Copyright (c) 2014年 dg11185. All rights reserved.
//

#import "LeftSideView.h"

#define DARKBLACK [UIColor colorWithRed:0 green:0 blue:0 alpha:0.9]
#define LIGHTBLACK [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]

@implementation LeftSideView

@synthesize delegate;

//初始化视图
-(LeftSideView*) initWithFrame:(CGRect)frame withArray:(NSArray*)_cityArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = DARKBLACK;
        //添加tableview
        leftTableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        leftTableView.dataSource = self;
        leftTableView.delegate = self;
        leftTableView.backgroundColor = DARKBLACK;
        leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:leftTableView];
        
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"分享",@"编辑地点", nil];
        [cityArray addObjectsFromArray:_cityArray];
        NSMutableArray *sectionArray2 = [NSMutableArray arrayWithObjects:@"设置",@"意见与建议",@"给此应用程序打分", nil];
        NSMutableArray *sectionArray3 = [NSMutableArray arrayWithObjects:@"YAHOO!", nil];
        dataArray = [NSMutableArray arrayWithObjects:cityArray,sectionArray2,sectionArray3,nil];
    }
    
    return self;
}

//块高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==1){
        return 50;
    }else{
        return 0.01;
    }
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==2) {
        return 100;
    }else{
        return 50;
    }
}
//块数
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return dataArray.count;
}
//行数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sectionArray = [dataArray objectAtIndex:section];
    return sectionArray.count;
}
//列表块视图
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
        view.backgroundColor = DARKBLACK;
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, view.frame.size.width-10, 50)];
        textLabel.textAlignment = NSTextAlignmentLeft;
        textLabel.textColor = [UIColor whiteColor];
        textLabel.text = @"工具";
        textLabel.backgroundColor = [UIColor clearColor];
        [view addSubview:textLabel];
        return view;
    }
    return nil;
}
//列表单元视图
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    if (indexPath.section==2) {
        //自定义
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.textColor = [UIColor whiteColor];
        textLabel.text = [[dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        [cell.contentView addSubview:textLabel];
        textLabel.backgroundColor = DARKBLACK;
        
    }else{
        cell.contentView.backgroundColor = LIGHTBLACK;
        cell.textLabel.text = [[dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        //分割线
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 49, tableView.frame.size.width-10, 1)];
        lineView.backgroundColor = LIGHTBLACK;
        [cell.contentView addSubview:lineView];

    }
    
    //设置选中时的颜色
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:110/255.0 green:75/255.0 blue:145/255.0 alpha:1];
    return cell;
}
//选中
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section==0 && indexPath.row>=2) {
        if (self.delegate) {
            [self.delegate selectCell:indexPath.row-2];
        }
    }
}

#pragma mark -Other
//添加城市
-(void) addCity:(NSString*)cityName{
    NSMutableArray *cityArray = [dataArray objectAtIndex:0];
    [cityArray addObject:cityName];
    [leftTableView reloadData];
}


@end
